#ifndef __MID_FSM_H__
#define __MID_FSM_H__

#include "main.h"
#include "lcd.h"
#include "board_bsp.h"
#include "ring_buffer.h"
#define TFT_TEXTSIZE 2
#define TFT_TEXT_LENGTH (7 * TFT_TEXTSIZE + 6)
#define TFT_TEXT_WIDTH (6 * TFT_TEXTSIZE)
#define TFT_UI_START_X 15
#define TFT_UI_START_Y 180

typedef enum
{
    FSM_ERROR = -1,
    FSM_NONE = 0,
    FSM_Menu,
    FSM_Cal,
    FSM_Cal_2,
    FSM_Cal_3,
    FSM_Cal_4,
    FSM_Cal_5,
    FSM_Info,
    FSM_Quick_Setup,
    FSM_Quick_Setup_2,
    FSM_Quick_Setup_3,
    FSM_Configure,
    FSM_System,
    FSM_Service,
    FSM_Messages,
    FSM_Calibration_Data,
    FSM_Model_Software_Revision,
    FSM_TOC_Sensor_Info,
    FSM_Measurement,
    FSM_Analog_Outputs,
    FSM_Set_Points,
    FSM_Alarm,
    FSM_TOC_Setup,
    FSM_Display,
    FSM_Flow_Control,
    FSM_Hold_outputs,
    FSM_Set_Language,
    FSM_USB,
    FSM_Passwords,
    FSM_Set_Clear_Lockout,
    FSM_Reset,
    FSM_Diagnostic,
    FSM_Calibrate,
    FSM_Tech_Service,

    FSM_Channel_Setup,
    FSM_Channel_Setup_1,
    FSM_Resistivity,
    FSM_Set_Averaging,

    FSM_UI_COUNT, // 状态总数
} FSM_UI;

typedef enum
{
    FSM_Event_NONE = 0,

    FSM_Event_LEFT_PRESS_UP,

    FSM_Event_RIGHT_PRESS_UP,

    FSM_Event_UP_PRESS_UP,

    FSM_Event_DOWN_PRESS_UP,

    FSM_Event_ENTER_PRESS_UP,

    FSM_Event_ESC,

    FSM_Event_COUNT, // 事件总数

} FSM_Event_t;

typedef enum
{
    Menu_Quick_Setup = 0, // 快速设置
    Menu_Service,         // 服务
    Menu_System,          // 系统
    Menu_Configure,       // 配置

    Menu_Event_COUNT, // 选项总数

} Menu_Option; // 菜单选项

typedef enum
{
    Cal_Analog_Pick = 0, // 模拟量选择
    Cal_4mA,             // 4mA校准
    Cal_20mA,            // 20mA校准
    Cal_Save_Enable,     // 保存校准
    Cal_Successful,      // 校准成功

    Cal_Event_COUNT, // 选项总数

} Cal_Option; // 校准选项

typedef enum
{
    Quick_parameter = 0, // 模拟量选择
    Quick_Analog,        // 4mA校准
    Quick_limit,         // 20mA校准
    Quick_Set_Point,     // 保存校准
    Quick_UV,            // 校准成功
    Quick_Save_Changes,

    Quick_Event_COUNT, // 选项总数

} Quick_Setup_Option; // 校准选项

typedef enum
{
    Mea_Channal_Setup = 0, // 模拟量选择
    Mea_Resistivity,       // 4mA校准
    Mea_Set_Averaging,     // 20mA校准

    Measurement_Event_COUNT, // 选项总数

} Measurement_Option; // 校准选项

typedef enum
{
    Channel_Setup_parameter = 0, // 通道选择参数
    Channel_Setup_a_b_Unit,      // a_b_通道选择
    Channel_Setup_c_d_Unit,      // c_d_通道选择
    Channel_Setup_Confirm,       // 确认

    Channel_Setup_Event_COUNT, // 选项总数

} Channel_Setup_Option; // 校准选项

typedef enum
{
    Channel_Setup_Class_None = 0,
    Channel_Setup_Class_TOC,

} Channel_Setup_Class; // 通道类型

typedef enum
{
    Resistivity_compensation = 0, // 电导率补偿

    Resistivity_Event_COUNT, // 选项总数

} Resistivity_Option; // 校准选项

typedef enum
{
    Set_Averaging_Setup = 0, // 平均设置
    Set_Averaging_Confirm,   // 确认

    Set_Averaging_Event_COUNT, // 选项总数

} Set_Averaging_Option; // 校准选项

/*消息事件回调函数指针类型 */
typedef void (*UnParam_UnRetern)(void);
typedef void (*VoidPtrParam_UnRetern)(void *);
typedef int (*VoidPtrParam_IntRetern)(void *);

typedef struct
{
    FSM_UI Now_State;       // 当前状态
    FSM_UI Menu_ui_Process; // 菜单ui进程

    Cal_Option Cal_ui_Process; // 校准ui进程
    int Cal_ui_Parameter;      // 校准ui参数
    int8_t Cal_ui_Cursor;      // 校准ui光标

    Quick_Setup_Option Quick_ui_Process; // 快速设置ui进程
    int8_t Quick_ui_Cursor;              // 快速设置ui光标
    int Quick_ui_Parameter;              // 校准ui参数
    int Quick_ui_Parameter2;             // 校准ui参数
    int Quick_ui_Parameter3;             // 校准ui参数

    FSM_UI Configure_ui_Process; // 配置ui进程
    FSM_UI System_ui_Process;    // 系统ui进程
    FSM_UI Service_ui_Process;   // 服务ui进程

    Measurement_Option Measurement_ui_Process; // 测量ui进程

    Channel_Setup_Option Channel_Setup_ui_Process;
    int8_t Channel_Setup_ui_Cursor;  // 通道选择ui光标
    int Channel_Setup_ui_Parameter;  // 通道选择ui参数
    int Channel_Setup_ui_Parameter2; // 通道选择ui参数
    int Channel_Setup_ui_Parameter3; // 通道选择ui参数
    int Channel_Setup_ui_Parameter4; // 通道选择ui参数

    Resistivity_Option Resistivity_ui_Process;
    Set_Averaging_Option Set_Averaging_ui_Process;

} FSMGlobal; // 全局状态机变量结构体

typedef struct
{
    uint8_t Cal_Aout_Num; // 校准模拟量选择
    int Cal_Aout1_4mA;    // 校准模拟量1 4mA
    int Cal_Aout1_20mA;   // 校准模拟量2 20mA
    int Cal_Aout2_4mA;    // 校准模拟量2 4mA
    int Cal_Aout2_20mA;   // 校准模拟量2 20mA

    int Quick_Num;           // 快速设置参数通道选择
    int Quick_Unit;          // 快速设置参数单位
    int Quick_analog_output; // 快速设置模拟输出使能

    int16_t Channel_Setup_a_measurements;    // a通道选择测量值
    int16_t Channel_Setup_a_unit_multiplier; // a通道选择单位乘数
    int16_t Channel_Setup_b_measurements;    // b通道选择测量值
    int16_t Channel_Setup_b_unit_multiplier; // b通道选择单位乘数
    int16_t Channel_Setup_c_measurements;    // c通道选择测量值
    int16_t Channel_Setup_c_unit_multiplier; // c通道选择单位乘数
    int16_t Channel_Setup_d_measurements;    // d通道选择测量值
    int16_t Channel_Setup_d_unit_multiplier; // d通道选择单位乘数

} FSMData; // 全局数据变量结构体

typedef struct
{
    uint8_t dummy;
} UI_NONE_Parameter_t; // FSM_NONE
typedef struct
{
    int16_t Menu_ui_Process; // 菜单ui进程
} UI_Menu_Parameter_t;       // FSM_Menu
typedef struct
{
    int UI_Cal_Aout_Num;
} UI_Cal_Parameter_t; // FSM_Cal
typedef struct
{
    int UI_Cal_Parameter;
} UI_Cal_2_Parameter_t; // FSM_Cal2
typedef struct
{
    int UI_Cal_Parameter;
} UI_Cal_3_Parameter_t; // FSM_Cal3
typedef struct
{
    int UI_Cal_Parameter;
} UI_Cal_4_Parameter_t; // FSM_Cal4
typedef struct
{
    int UI_Cal_Parameter;
} UI_Cal_5_Parameter_t; // FSM_Cal5

typedef struct
{
    int UI_Info_Parameter;
} UI_Info_Parameter_t; // FSM_Info
typedef struct
{
    int UI_Quick_Setup_Parameter;
    int UI_Quick_Setup_Parameter_2;
    int UI_Quick_Setup_Parameter_3;
} UI_Quick_Setup_Parameter_t; // FSM_Quick_Setup
typedef struct
{
    int UI_Configure_Parameter;
} UI_Configure_Parameter_t; // FSM_Configure
typedef struct
{
    int UI_System_Parameter;
} UI_System_Parameter_t; // FSM_System
typedef struct
{
    int UI_Service_Parameter;
} UI_Service_Parameter_t; // FSM_Service
typedef struct
{
    int UI_Messages_Parameter;
} UI_Messages_Parameter_t; // FSM_Messages
typedef struct
{
    int UI_Calibration_Data_Parameter;
} UI_Calibration_Data_Parameter_t; // FSM_Calibration_Data
typedef struct
{
    int UI_Model_Software_Revision_Parameter;
} UI_Model_Software_Revision_Parameter_t; // FSM_Model_Software_Revision
typedef struct
{
    int UI_TOC_Sensor_Info_Parameter;
} UI_TOC_Sensor_Info_Parameter_t; // FSM_TOC_Sensor_Info
typedef struct
{
    int UI_Measurement_Parameter;
} UI_Measurement_Parameter_t; // FSM_Measurement
typedef struct
{
    int UI_Analog_Outputs_Parameter;
} UI_Analog_Outputs_Parameter_t; // FSM_Analog_Outputs
typedef struct
{
    int UI_Set_Points_Parameter;
} UI_Set_Points_Parameter_t; // FSM_Set_Points
typedef struct
{
    int UI_Alarm_Parameter;
} UI_Alarm_Parameter_t; // FSM_Alarm
typedef struct
{
    int UI_TOC_Setup_Parameter;
} UI_TOC_Setup_Parameter_t; // FSM_TOC_Setup
typedef struct
{
    int UI_Display_Parameter;
} UI_Display_Parameter_t; // FSM_Display
typedef struct
{
    int UI_Flow_Control_Parameter;
} UI_Flow_Control_Parameter_t; // FSM_Flow_Control
typedef struct
{
    int UI_Hold_outputs_Parameter;
} UI_Hold_outputs_Parameter_t; // FSM_Hold_outputs
typedef struct
{
    int UI_Set_Language_Parameter;
} UI_Set_Language_Parameter_t; // FSM_Set_Language
typedef struct
{
    int UI_USB_Parameter;
} UI_USB_Parameter_t; // FSM_USB
typedef struct
{
    int UI_Passwords_Parameter;
} UI_Passwords_Parameter_t; // FSM_Passwords
typedef struct
{
    int UI_Set_Clear_Lockout_Parameter;
} UI_Set_Clear_Lockout_Parameter_t; // FSM_Set_Clear_Lockout
typedef struct
{
    int UI_Reset_Parameter;
} UI_Reset_Parameter_t; // FSM_Reset
typedef struct
{
    int UI_Diagnostic_Parameter;
} UI_Diagnostic_Parameter_t; // FSM_Diagnostic
typedef struct
{
    int UI_Calibrate_Parameter;
} UI_Calibrate_Parameter_t; // FSM_Calibrate
typedef struct
{
    int UI_Tech_Service_Parameter;
} UI_Tech_Service_Parameter_t; // FSM_Tech_Service
typedef struct
{
    uint8_t none;
} UI_Channel_Setup_Parameter_t; // FSM_Channel_Setup
typedef struct
{
    struct
    {
        enum Line_Class{
            None = 0,
            TOC,
            gC_L,
            o_cm,
            S_cm,
            S_m,
            oC,
            oF,
            LampRemain,
            CLASS_LEN,
        } class;
        enum{
            NONE = 0,

        }unit;
    }line[2];
    
} UI_Channel_Setup_1_Parameter_t; // FSM_Channel_Setup_1
typedef struct
{
    int UI_Resistivity_Parameter;
} UI_Resistivity_Parameter_t; // FSM_Resistivity
typedef struct
{
    int UI_Set_Averaging_Parameter;
} UI_Set_Averaging_Parameter_t; // FSM_Set_Averaging

// 界面处理项
typedef struct
{
    FSM_UI UI_id;                            // 界面
    void *Parameter;                         // 参数
    int8_t Cursor;                           // 光标
    void (*struct_create)(void);             // 构造函数
    void (*handle_state_while)(void);        // 状态函数
    void (*handle_event)(FSM_Event_t event); // 事件函数
    void (*struct_destroy)(void);            // 析构函数
} UI_process_t;

// 界面管理器
typedef struct
{
    UI_process_t *current_screen; // 当前界面
    FSM_UI current_screen_id;     // 当前界面ID
} TFT_UIManager;

// 界面参数项
typedef struct
{
    UI_NONE_Parameter_t None_Parameter;                                       // FSM_NONE
    UI_Menu_Parameter_t Menu_Parameter;                                       // FSM_Menu
    UI_Cal_Parameter_t Cal_Parameter;                                         // FSM_Cal
    UI_Cal_Parameter_t Cal_Parameter_2;                                       // FSM_Cal_2
    UI_Cal_Parameter_t Cal_Parameter_3;                                       // FSM_Cal_3
    UI_Cal_Parameter_t Cal_Parameter_4;                                       // FSM_Cal_4
    UI_Cal_Parameter_t Cal_Parameter_5;                                       // FSM_Cal_5
    UI_Info_Parameter_t Info_Parameter;                                       // FSM_Info
    UI_Quick_Setup_Parameter_t Quick_Setup_Parameter;                         // FSM_Quick_Setup
    UI_Quick_Setup_Parameter_t Quick_Setup_Parameter_2;                       // FSM_Quick_Setup_2
    UI_Quick_Setup_Parameter_t Quick_Setup_Parameter_3;                       // FSM_Quick_Setup_3
    UI_Configure_Parameter_t Configure_Parameter;                             // FSM_Configure
    UI_System_Parameter_t System_Parameter;                                   // FSM_System
    UI_Service_Parameter_t Service_Parameter;                                 // FSM_Service
    UI_Messages_Parameter_t Messages_Parameter;                               // FSM_Messages
    UI_Calibration_Data_Parameter_t Calibration_Data_Parameter;               // FSM_Calibration_Data
    UI_Model_Software_Revision_Parameter_t Model_Software_Revision_Parameter; // FSM_Model_Software_Revision
    UI_TOC_Sensor_Info_Parameter_t TOC_Sensor_Info_Parameter;                 // FSM_TOC_Sensor_Info
    UI_Measurement_Parameter_t Measurement_Parameter;                         // FSM_Measurement
    UI_Analog_Outputs_Parameter_t Analog_Outputs_Parameter;                   // FSM_Analog_Outputs
    UI_Set_Points_Parameter_t Set_Points_Parameter;                           // FSM_Set_Points
    UI_Alarm_Parameter_t Alarm_Parameter;                                     // FSM_Alarm
    UI_TOC_Setup_Parameter_t TOC_Setup_Parameter;                             // FSM_TOC_Setup
    UI_Display_Parameter_t Display_Parameter;                                 // FSM_Display
    UI_Flow_Control_Parameter_t Flow_Control_Parameter;                       // FSM_Flow_Control
    UI_Hold_outputs_Parameter_t Hold_outputs_Parameter;                       // FSM_Hold_outputs
    UI_Set_Language_Parameter_t Set_Language_Parameter;                       // FSM_Set_Language
    UI_USB_Parameter_t USB_Parameter;                                         // FSM_USB
    UI_Passwords_Parameter_t Passwords_Parameter;                             // FSM_Passwords
    UI_Set_Clear_Lockout_Parameter_t Set_Clear_Lockout_Parameter;             // FSM_Set_Clear_Lockout
    UI_Reset_Parameter_t Reset_Parameter;                                     // FSM_Reset
    UI_Diagnostic_Parameter_t Diagnostic_Parameter;                           // FSM_Diagnostic
    UI_Calibrate_Parameter_t Calibrate_Parameter;                             // FSM_Calibrate
    UI_Tech_Service_Parameter_t Tech_Service_Parameter;                       // FSM_Tech_Service
    UI_Channel_Setup_Parameter_t Channel_Setup_Parameter;                     // FSM_Channel_Setup
    UI_Channel_Setup_1_Parameter_t Channel_Setup_1_Parameter;                 // FSM_Channel_Setup
    UI_Resistivity_Parameter_t Resistivity_Parameter;                         // FSM_Resistivity
    UI_Set_Averaging_Parameter_t Set_Averaging_Parameter;                     // FSM_Set_Averaging
} UI_Parameter_t;

#define FSM_Messages_MAX 3

void FSM_Cal_Successful_handler(void); // 校准成功处理函数

FSM_UI get_FSM_UI(void);

// 以字为单位写字符串
void Menu_WriteStr(uint8_t y_line, uint8_t x_char_start, char *str);
// 以字为单位写字符串
void Menu_WriteChar(uint8_t y_line, uint8_t x_char_start, char Writechar);
// 以字为单位清除字符
void Menu_ClearChar(uint8_t y_line, uint8_t x_char_start, uint8_t char_num);
// 写菜单箭头
void TFT_menu_Arrow_write(char *str);
// 清除菜单箭头
void TFT_menu_Arrow_Clear(void);
// 清除所有
void TFT_menu_ALL_Clear(void);

void TFT_UIManager_init(void);
void state_machine_run(void);
void event_machine_run(FSM_Event_t event);
void UI_change_run(FSM_UI next_UI_id);

#endif
